
package com.trydofor.a9task.base.sys.help;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.trydofor.aloe.exception.Exceptions;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 * @date : Dec 11, 2012
 * @see :
 */

public class StaticFieldFilter<T> implements FieldFilter {
    
    private final List<T>  val;
    private final Class<T> clz;
    
    public static <C> StaticFieldFilter<C> of(Class<C> clz) {
        return new StaticFieldFilter<C>(clz);
    }
    
    private StaticFieldFilter(Class<T> clz) {
        this.val = new ArrayList<T>();
        this.clz = clz;
    }
    
    public List<T> values() {
        return val;
    }
    
    @Override
    public void filter(Field fd) {
        if (fd.getType().isAssignableFrom(clz)) {
            try {
                @SuppressWarnings("unchecked")
                T c = (T) fd.get(null);
                val.add(c);
            }
            catch (Exception e) {
                Exceptions.throwFakeException(e);
            }
        }
    }
}
